package com.codeest.geeknews.ui.main.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.alexvasilkov.gestures.GestureController;
import com.alexvasilkov.gestures.commons.RecyclePagerAdapter;
import com.alexvasilkov.gestures.views.GestureImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeest.geeknews.R;
import com.codeest.geeknews.base.GestureImageViewSetting;
import com.codeest.geeknews.model.bean.PhotoBean;
import com.codeest.geeknews.ui.main.activity.PhotoDetailActivity;

import java.util.List;

/**
 * Created by admin on 2017/8/3.
 */

public class PaintingsPagerAdapter extends RecyclePagerAdapter<PaintingsPagerAdapter.ViewHolder> {

    private final ViewPager viewPager;
    private final List<PhotoBean> imageInfos;
    private final GestureImageViewSetting setting;
    private final PhotoDetailActivity.ImageLoadCallback imageLoadCallback;
    private OnSingleTapUpListener imageClickListener;
    //private Bitmap bitmap;
    private int mPositon;


    public PaintingsPagerAdapter(ViewPager pager, List<PhotoBean> imageInfos,
                                 GestureImageViewSetting setting, PhotoDetailActivity.ImageLoadCallback imageLoadCallback) {
        this.viewPager = pager;
        this.imageInfos = imageInfos;
        this.setting = setting;
        this.imageLoadCallback = imageLoadCallback;
    }

    /**
     * 传Bitmap bitmap, int positon 直接设置背景，避免闪烁
     *
     * @param pager-
     * @param imageInfos-
     * @param bitmap-
     * @param positon-
     * @param setting-
     * @param imageLoadCallback-
     */
    public PaintingsPagerAdapter(ViewPager pager, List<PhotoBean> imageInfos, Bitmap bitmap, int positon,
                                 GestureImageViewSetting setting, PhotoDetailActivity.ImageLoadCallback imageLoadCallback) {
        this.viewPager = pager;
        this.imageInfos = imageInfos;
       // this.bitmap = bitmap;
        this.mPositon = positon;
        this.setting = setting;
        this.imageLoadCallback = imageLoadCallback;
    }

    /**
     * 传Bitmap bitmap, int positon 直接设置背景，避免闪烁
     *
     * @param pager-
     * @param imageInfos-
     * //@param bitmap-
     * @param positon-
     * @param setting-
     * @param imageLoadCallback-
     * @param imageClickListener-
     */
    public PaintingsPagerAdapter(ViewPager pager, List<PhotoBean> imageInfos,/* Bitmap bitmap, */int positon,
                                 GestureImageViewSetting setting, PhotoDetailActivity.ImageLoadCallback imageLoadCallback, OnSingleTapUpListener imageClickListener) {
        this.viewPager = pager;
        this.imageInfos = imageInfos;
        //this.bitmap = bitmap;
        this.mPositon = positon;
        this.setting = setting;
        this.imageLoadCallback = imageLoadCallback;
        this.imageClickListener = imageClickListener;
    }

    private View mCurrentView;

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        mCurrentView = ((ViewHolder) object).itemView;
    }

    public View getPrimaryItem() {
        return mCurrentView;
    }

    @Override
    public int getCount() {
        return imageInfos.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup container) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_photo_detail, null);
        ViewHolder holder = new ViewHolder(container, setting, /*view, */imageClickListener);
        holder.image.getController().enableScrollInViewPager(viewPager);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
//        if (setupListener != null) {
//            setupListener.onSetupGestureView(holder.image);
//        }
//        if (mPositon == position) {
//            holder.image.setImageBitmap(bitmap);
//        }
        /*holder.image.post(new Runnable() {
            @Override
            public void run() {
                Glide.with(holder.image.getContext())
                        .load(imageInfos.get(position).getUrl())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>(holder.image.getWidth() / 2, holder.image.getHeight() / 2) {
                            //                        .into(new SimpleTarget<Bitmap>(100, 100) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                holder.image.setImageBitmap(resource);
                                if (imageLoadCallback != null) {
                                    imageLoadCallback.onLoaded(position, resource);
                                }
                            }
                        });
            }
        });*/
        Glide.with(holder.image.getContext())
                .load(imageInfos.get(position).getUrl())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.image);

    }

    public static GestureImageView getImage(RecyclePagerAdapter.ViewHolder holder) {
        return ((ViewHolder) holder).image;
    }

    static class ViewHolder extends RecyclePagerAdapter.ViewHolder {
        final GestureImageView image;
        OnSingleTapUpListener onSingleTapUpListener;
        GestureImageViewSetting setting;

        ViewHolder(ViewGroup container, GestureImageViewSetting setting, /*View view, */OnSingleTapUpListener imageClickListener) {
            super(new GestureImageView(container.getContext()));
            image = (GestureImageView) itemView;
            this.setting = setting;
            if (setting != null) {
                image.getController().getSettings()
                        .setPanEnabled(setting.isPanEnabled())
                        .setZoomEnabled(setting.isZoomEnabled())
                        .setDoubleTapEnabled(setting.isZoomEnabled())
                        .setRotationEnabled(setting.isRotationEnabled())
                        .setRestrictRotation(setting.isRestrictRotation())
                        .setOverscrollDistance(container.getContext(), setting.getOverscrollX(), setting.getOverscrollY())
                        .setOverzoomFactor(setting.getOverzoom())
                        .setExitEnabled(setting.isExitEnabled())
                        .setFillViewport(setting.isFillViewport())
                        .setFitMethod(setting.getFitMethod())
                        .setGravity(setting.getGravity())
                        .setAnimationsDuration(setting.getDuration());

            }

            this.onSingleTapUpListener = imageClickListener;
            image.getController().setOnGesturesListener(new GestureController.OnGestureListener() {
                @Override
                public void onDown(@NonNull MotionEvent e) {

                }

                @Override
                public void onUpOrCancel(@NonNull MotionEvent e) {

                }

                @Override
                public boolean onSingleTapUp(@NonNull MotionEvent e) {
                    return false;
                }

                @Override
                public boolean onSingleTapConfirmed(@NonNull MotionEvent e) {
                    if (onSingleTapUpListener != null) {
                        onSingleTapUpListener.onSingleTapConfirmed(image);
                    }
                    return true;
                }

                @Override
                public void onLongPress(@NonNull MotionEvent e) {

                }

                @Override
                public boolean onDoubleTap(@NonNull MotionEvent e) {
                    return false;
                }
            });
        }
    }

    public interface OnSingleTapUpListener {
        void onSingleTapConfirmed(View view);
    }

}